package net.sharpf2.api.util;

import java.awt.event.KeyEvent;
import net.sharpf2.api.content.WorldObject;
import net.sharpf2.api.Globals;
import net.sharpf2.api.Methods;
import net.sharpf2.api.types.Content;
import net.sharpf2.sfcore.client.Npc;
import net.sharpf2.sfcore.input.Keyboard;

/**
 * Handling the camera
 * @author Sam Smekens
 * 
 * TODO: setPitch and getPitch
 */
public class Camera {

    private static Keyboard keyboard = Globals.CONTROLLER.getKeyboard();

    /**
     * Turns the cam
     * @param d Degrees
     */
    public static void turnTo(int d) {
        if(d <= 360 && d >= 0){
           do {
                keyboard.sendKey(getAngleDirectionChar(d), Mathematics.random(10, 50));
            } while (getCurrentAngle() <= d - 5 || getCurrentAngle() >= d + 5 && !Thread.currentThread().isInterrupted());
        }
    }

    /**
     * Turns to an object
     * @param o Object where camera should turn to.
     */
    public static void turnToObject(Content<WorldObject> o) {
        if (o.getContent().getModel() != null) {
            turnTo(Mathematics.tileToAngle(o.getContent().getWorldPosition()) - 90);
        }
    }
    
    /**
     * Turns to a npc
     * @param npc Npc where camera should turn to.
     */
    public static void turnToNpc(Content<Npc> npc) {
        if (npc.getContent().getModel() != null) {
            turnTo(Mathematics.tileToAngle(npc.getContent().getWorldPosition()) - 90);
        }
    }

    /**
     * Sets the pitch to 100% up or 100% down
     * @param up
     */
    public static void setPitch(boolean up) {
        keyboard.sendKey(up ? (char) KeyEvent.VK_UP : (char) KeyEvent.VK_DOWN, 5000);
    }

    /**
     * Gets the shortest direction to turn to
     * @param   d Destination angle
     * @return  char key-left or key-right
     */
    private static char getAngleDirectionChar(int d) {
        int way1 = 360 - getCurrentAngle();
        int way2 = 360 - d;

        if (way1 <= way2) {
            return KeyEvent.VK_LEFT;
        } else {
            return KeyEvent.VK_RIGHT;
        }
    }
    
    /**
     * Gets the shortest direction to turn to
     * @param   d Destination angle
     * @return  char key-left or key-right
     */
    private static int getAngleDirectionInt(int d) {
        int way1 = 360 - getCurrentAngle();
        int way2 = 360 - d;

        if (way1 <= way2) {
            return 37;
        } else {
            return 39;
        }
    }

    /**
     * Returns the current camera angle
     * Starting from 0 to 360 degrees
     * @return int Current camera angle
     */
    public static int getCurrentAngle() {
        return (int) Math.round((Methods.getClient().getCameraYaw() * 360) / Math.pow(2.00, 14.00));
    }

    /**
     * Gets current pitch.
     * @return Pitch
     */
    public static int getCurrentPitch() {
        return -1;//return Methods.getClient()...
    }
    
    /**
    * Gives correct degrees for winds
    */
    public static class Compass {
        public static final int NORTH = 360;
        public static final int EAST = 270;
        public static final int SOUTH = 180;
        public static final int WEST = 90;
    }
}
